<template>
    <div class="workable-workableTask-container">
        <el-dialog title="部署计划任务" v-model="isShowDialog" >
            <el-form ref="deployWorkableFormRef" :model="formData" :rules="formRules" size="small" label-width="100px">
                <el-row :gutter="35">
                    <el-col :xs="24" :sm="6" :md="6" :lg="6" :xl="6" class="mb20">
                        <el-form-item label="主要版本号" prop="major">
                            <el-input-number v-model="formData.major" :min="1" :max="9" clearable class="w100"></el-input-number>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="6" :md="6" :lg="6" :xl="6" class="mb20">
                        <el-form-item label="次要版本号" prop="minor">
                            <el-input-number v-model="formData.minor" :min="0" :max="9" clearable class="w100"></el-input-number>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="6" :md="6" :lg="6" :xl="6" class="mb20">
                        <el-form-item label="补丁版本号" prop="patch">
                            <el-input-number v-model="formData.patch" :min="0" :max="9" clearable class="w100"></el-input-number>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="6" :md="6" :lg="6" :xl="6" class="mb20">
                        <el-form-item label="构建版本号" prop="build">
                            <el-input-number v-model="formData.build" :min="0" :max="9" clearable class="w100"></el-input-number>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="部署版本号" prop="major">
							<span class="color-primary">{{formData.major}}.{{formData.minor}}.{{formData.patch}}.{{formData.build}}</span>
						</el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="onCancel" size="small">取 消</el-button>
                    <el-button type="primary" @click="onSubmit" size="small">部 署</el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>

<script lang="ts">
import { reactive, toRefs, ref } from 'vue';
import { ElMessage } from 'element-plus';
import { deployWorkableTask } from '/@/api/workable/task/index';
export default {
    name: 'workable/task/deploy',
    setup(props, context) {
        const deployWorkableFormRef = ref();
        const state = reactive({
            isShowDialog: false,
            formData: {
				ids: [],
				major: 1,
				minor: 0,
				patch: 0,
				build: 0,
            },
            formRules : {
				
            }
        });
        
        // 打开弹窗
        const openDialog = (row?: object) => {
            state.isShowDialog = true;
			state.formData.ids = row.ids;
        };
        // 关闭弹窗
        const closeDialog = (row?: object) => {
            state.isShowDialog = false;
            for(var key in state.formData) {
                state.formData[key] = undefined;
            }
            deployWorkableFormRef.value.resetFields();
        };
        // 取消
        const onCancel = () => {
            closeDialog();
        };
        // 部署
        const onSubmit = () => {
            deployWorkableFormRef.value.validate(valid => {
                if(valid){
                    let params = {
                        ids: state.formData.ids,
						version: {
							major: state.formData.major,
							minor: state.formData.minor,
							patch: state.formData.patch,
							build: state.formData.build,
						}
                    }
                    deployWorkableTask(params).then((res) => {
                        if(res.code === 0){
                            ElMessage.success('部署成功');
                            context.emit('submit');
                            closeDialog();
                        }
                    });
                }
            });
        };
        return {
            deployWorkableFormRef,
            openDialog,
            closeDialog,
            onCancel,
            onSubmit,
            ...toRefs(state),
        };
    },
};
</script>